﻿using STHPMS1.BLL.B_Core;
using STHPMS1.Common.C_Entity.Stmpms1;
using STHPMS1.Common.C_Enum;
using STHPMS1.ICore;
using STHPMS1.ICore.I_BLL.Bus;
using STHPMS1.ICore.I_Entity.Info;
using STHPMS1.ICore.I_Entity.Stmpms1;
using STHPMS1.ICore.I_Serv;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using STHPMS1.Common.C_Extension;
using STHPMS1.ICore.I_DAL;
using System.Data;
using System.Linq;
using STHPMS1.Common.C_Utils;

namespace STHPMS1.BLL.B_Bus
{
    public class B_HistoryBus : BllBase, IB_HistoryBll
    {
        public IEnumerable<T> Get<T>(Expression<Func<T, bool>> predicate, IPagingInfo info) where T : class, IB_History_Details
        {
            return this.Scene.Dal.Sthpms1.Select<T>(predicate, info).OrderBy(q => q.Year);
        }

        public IEnumerable<T> GetDetails<T>(Expression<Func<T, bool>> predicate, IPagingInfo info,string id) where T : class, IB_History_Layer_Details
        {
            return this.Scene.Dal.Sthpms1.Select<T>(predicate, info).Where(q => q.Detailsid==id);
        }

        public IEnumerable<T> GetPMSTC<T>(string rdid, int dir) where T : class, IB_Pmstc
        {
            ISthpms1Dao dao = this.Scene.Dal.Sthpms1;
            return dao.Select<T>(q => q.Rdwayid == rdid && q.Direction_Dictid == dir).OrderBy(q => q.Endmp);
        }

        public IEnumerable<T> GetLayer<T>(string struid) where T : class,IB_Layer
        {
            ISthpms1Dao dao = this.Scene.Dal.Sthpms1;
            return dao.Select<T>(q => q.Struid==struid);
        }

         public void UpdateHistory(string oldStruid,string newStruid,double mp)
        {
            this.Scene.Dal.Sthpms1.B_History.UpdateHistory(oldStruid, newStruid, mp);
        }

        public void UpdateEndmp(string struid,double endmp)
         {
            this.Scene.Dal.Sthpms1.Update<B_Pmstc>(new { Endmp = endmp }, q => q.Struid == struid);
         }

        public void MoveOldData(string oldStruid)
        {
            this.Scene.Dal.Sthpms1.B_History.MoveOldData(oldStruid);
        }

        public void AddNewData(string oldStruid, IB_Pmstc p)
        {
            this.Scene.Dal.Sthpms1.B_History.AddNewData(oldStruid, p);
        }

        public void UpdateXTHistroy(IB_Pmstc p, IB_Layer[] lstLayer)
        {
            this.Scene.Dal.Sthpms1.B_History.UpdateXTHistroy(p, lstLayer);
        }

        public void UpdateHistory(string oldStruid, IB_Pmstc[] lstPms, IB_Layer[] lstLayer)
        {
            this.Scene.Dal.Sthpms1.B_History.UpdateHistory(oldStruid, lstPms, lstLayer);
        }

        public void AddHistory(IB_History_Details h_ds, IB_History_Layer_Details[] h_l_ds)
        {
            this.Scene.Dal.Sthpms1.B_History.AddHistory(h_ds, h_l_ds);
        }

        public bool ExistsLayer(string rdId,int dir,int layerDictid)
        {
            ISthpms1Dao dao = this.Scene.Dal.Sthpms1;
            return dao.Exists<B_Layer>(q => q.Rdwayid == rdId && q.Direction_Dictid==dir && q.Layer_Dictid==layerDictid);
        }

        /// <summary>
        /// 更新预养护措施
        /// </summary>
        /// <param name="rdwayid"></param>
        /// <param name="beginmp"></param>
        /// <param name="endmp"></param>
        /// <param name="direct"></param>
        /// <param name="Measure"></param>
        public void UpdatePreventModel(string rdwayid, double beginmp, double endmp, int direct, string measure)
        {
            ISthpms1Dao dao = this.Scene.Dal.Sthpms1;
            List<B_Invntory> invList=dao.Select<B_Invntory>(p => p.Rdwayid == rdwayid &&
                                                          p.Direction_Dictid == direct &&
                                                          (p.Beginmp>=beginmp && p.Endmp<=endmp));

            double pci = 0;
            List<Dm_Modelgroup> models = new List<Dm_Modelgroup>();

            foreach (var item in invList)
            {
                var pList=dao.Select<Dm_Histperf>(p => p.Segid == item.Segid, null, k => k.Perfyear, true);//取上一年度的pci值
                if (pList.Count == 0) throw new Exception("路段的模型校定指标数据不存在-"+item.Name);
                pci += pList[0].Pciave??0;

                B_Pmstc pmstc = dao.Single<B_Pmstc>(p => p.Rdwayid == item.Rdwayid &&
                                                     p.Direction_Dictid == item.Direction_Dictid &&
                                                    (p.Beginmp <= item.Beginmp && p.Endmp >= item.Endmp));//求出路段的面层结构
                if (pmstc == null) throw new Exception("路段的路面结构没有找到-"+item.Name);

                Dm_Modelgroup model=dao.Single<Dm_Modelgroup>(p => p.Pvmttype_Dictid == pmstc.Pvmttype_Dictid &&
                                                                p.Basictype_Dictid == pmstc.Basictype_Dictid &&
                                                                p.Modeltype_Dictid == MainTypeEnum.M12001.GetEnumValue() &&
                                                                (pmstc.Thick_S >= p.Thick_Min && pmstc.Thick_S <= p.Thick_Max) &&
                                                                 p.Comment == measure);

                if (model == null) throw new Exception(string.Format("路段的预养护模型没有找到-{0}_{1}_{2}" ,item.Name,pmstc.Struid,measure));
                if (models.Exists(p => p.Modelgroup_Id == model.Modelgroup_Id)) continue;
                models.Add(model);
            }

            pci = pci / invList.Count;

            string group2Names = "稀浆封层,微表处,碎石封层,复合封层,纤维沥青碎石封层,开普封层";
            string group3Names = "雷诺锋,沥青还原剂,雾封层,含砂雾封层";
            foreach (var model in models)
            {
                double b=0.7d;
                if (group2Names.Contains(measure))
                {
                    b = 0.9;
                }
                if (model.Formulacoeff2 == null)//只有在空的情况下才更新模型系数
                    model.Formulacoeff2 = pci + (100 - pci) * b;
            }

            if (models.Count>0)dao.SaveAll(models);
        }
    }
}
